package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysFileRsp;
import com.ruoyi.system.domain.SystemFile;
import com.ruoyi.system.service.ISysFileRspService;
import com.ruoyi.system.service.ISysFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件管理Controller
 *
 * @author ruoyi
 * @date 2025-01-15
 */
@RestController
@RequestMapping("/system/file")
public class SysFileController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private ISysFileService sysFileService;

    @Autowired
    private ISysFileRspService sysFileRspService;

    /**
     * 查询文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(SystemFile systemFile) {
        startPage();
        List<SystemFile> list = sysFileService.selectSysFileList(systemFile);
        return getDataTable(list);
    }

    /**
     * 导出文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SystemFile systemFile) {
        List<SystemFile> list = sysFileService.selectSysFileList(systemFile);
        ExcelUtil<SystemFile> util = new ExcelUtil<SystemFile>(SystemFile.class);
        util.exportExcel(response, list, "文件管理数据");
    }

    /**
     * 获取文件管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysFileService.selectSysFileById(id));
    }

    /**
     * 新增文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SystemFile systemFile) {
        return toAjax(sysFileService.insertSysFile(systemFile));
    }

    @Log(title = "中移接口返回", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        // 上传并返回新文件名称
        String filePath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
        SystemFile systemFile = new SystemFile();
        systemFile.setFilePath(filePath);
        systemFile.setFileName(file.getOriginalFilename());
        ExcelUtil<SysFileRsp> util = new ExcelUtil<SysFileRsp>(SysFileRsp.class);
        List<SysFileRsp> sfs = util.importExcel(file.getInputStream());
        systemFile.setNum(sfs.size());
        int i = sysFileService.insertSysFile(systemFile);
        Long fileId = systemFile.getId();
        log.info("SysFileRsp:{}", sfs);
        String message = sysFileRspService.importPhone(sfs, updateSupport, fileId);

        return success(message);
    }

    @Log(title = "中移接口返回", businessType = BusinessType.IMPORT)
    @PostMapping("/importExcel")
    public AjaxResult importExcel(MultipartFile file) throws Exception {
        // 上传并返回新文件名称
        String filePath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
        SystemFile systemFile = new SystemFile();
        systemFile.setFilePath(filePath);
        systemFile.setFileName(file.getOriginalFilename());
        ExcelUtil<SysFileRsp> util = new ExcelUtil<SysFileRsp>(SysFileRsp.class);
        List<SysFileRsp> sfs = util.importExcel(file.getInputStream());
        systemFile.setNum(sfs.size());
        int i = sysFileService.insertSysFile(systemFile);
        Long fileId = systemFile.getId();
        log.info("SysFileRsp:{}", sfs);
        String message = sysFileRspService.importPhone(sfs, fileId);

        return success(message);
    }

    @Log(title = "中移接口返回", businessType = BusinessType.IMPORT)
    @PostMapping("/uploadData")
    public AjaxResult uploadData(MultipartFile file) throws Exception {
        // 上传并返回新文件名称
        String filePath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
        SystemFile systemFile = new SystemFile();
        systemFile.setFilePath(filePath);
        systemFile.setFileName(file.getOriginalFilename());
        int i = sysFileService.insertSysFile(systemFile);
        Long fileId = systemFile.getId();
        String message = sysFileRspService.uploadData(file, fileId);
        return success(message);
    }

    /**
     * 修改文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SystemFile systemFile) {
        return toAjax(sysFileService.updateSysFile(systemFile));
    }

    /**
     * 删除文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysFileService.deleteSysFileByIds(ids));
    }
}
